package ModelLayer;
import java.util.ArrayList; //The use of ArrayList is needed because there can be
//several copies for each DVD.
public class DVD
{
    private String dvdId; //This is an id for the dvd and it is unique.
    private String title;
    private String artist;
    private String publicationDate;
    private ArrayList<Copy> copiesDvd;
    
    /** CONSTRUCTORS */
    public DVD (String dvdId, String title, String artist, String publicationDate)
    {
        this.dvdId = dvdId;
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
        
        copiesDvd = new ArrayList<Copy>();
    }
    
    /** ACCESSORS */
    public String getDvdId()
    {
        return dvdId;
    }
    public String getTitle()
    {
        return title;
    }
    public String getArtist()
    {
        return artist;
    }
    public String getPublicationDate()
    {
        return publicationDate;
    }
    public ArrayList<Copy> copiesCollection(){
        if(copiesDvd== null){
            System.out.println("No copies of DVD at the time.");
        }
         return copiesDvd;
    }
    /** MUTATORS */
    public void setDvdId(String dvdId)
    {
        this.dvdId = dvdId;
    }
    public void setTitle(String Title)
    {
        this.title = Title;
    }
    public void setArtist(String Artist)
    {
        this.artist = Artist;
    }
    public void setPublicationDate(String PublicationDate)
    {
        this.publicationDate = PublicationDate;
    }
    public void addCopy(Copy myCopy)
    {
        copiesDvd.add(myCopy);
    }
    public void createCopy(String serialNumber, String purchaseDate, String purchasePrice)
    {
            copiesDvd.add(new Copy(serialNumber,purchaseDate,purchasePrice));
    }
    public void removeCopy(String serialNumber){
            copiesDvd.remove(findCopy(serialNumber));
    }
    public void update(String title, String artist, String publicationDate)
    {
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
        
    }
    public String toString() // returns a string with atributes of a dvd
    {
        String s="Title=" + getTitle()+'\n'+ "Artist=" +getArtist()+ '\n' +"ID="+getDvdId()+'\n' + "PublicationDate=" + getPublicationDate()+'\n';
        return s;
    }
    public Copy findCopy(String serialNumber) {
        for(int i=0; i<copiesDvd.size();i++){
            if(serialNumber.equals(copiesDvd.get(i).getSerialNumber())){
                return copiesDvd.get(i);
            }
        }
        return null;
    }
    public void updateCopies(String serialNumber, String purchaseDate, String purchasePrice){
            findCopy(serialNumber).update(purchaseDate, purchasePrice);
    }
    public Copy findUnrentedCopy()
    {
        int index=0;
        boolean found= false;
        Copy c=null;
        while(!found && index<copiesDvd.size())
        {
            if ( !copiesDvd.get(index).isRented())
            {
            c=copiesDvd.get(index);
            copiesDvd.get(index).rentCopy();
            found=true;
            }
            index++;
        }
        if(c==null)
        System.out.println("There are no copies for dvd: " + getTitle()); 
        return c;
    }
}
